
`include "common_header.verilog"

//  *************************************************************************
//  File : dec_func_xgxs.v
//  *************************************************************************
//  This program is controlled by a written license agreement.
//  Unauthorized reproduction or use is expressly prohibited. 
//  Copyright (c) 2002-2003 Morethanip
//  An der Steinernen Brueke 1, 85757 Karlsfeld, Germany
//  info@morethanip.com
//  http://www.morethanip.com
//  *************************************************************************
//  Designed by : Sebastien Marcellier
//  fbalay@morethanip.com
//  *************************************************************************
//  Decription : Decoder function (Transmission code 8b/10b)
//  Version    : $Id: dec_func_xgxs.v,v 1.1 2011/03/29 10:37:02 mr Exp $
//  *************************************************************************\

module dec_func_xgxs (

   din,
   sync,
   ce,
   dout,
   kout,
   rd_in,
   rd_out,
   char_err,
   disp_err,
   clk,
  `ifdef USE_CLK_ENA
   clk_ena,
  `endif   
   rst);
   
input   [9:0] din;      //  Parallel byte of incoming data        
input   sync;           //  sync achieved by aligner
input   ce;             //  Asserted when input data are stable        
output  [7:0] dout;     //  Parallel output data        
output  kout;           //  Special caracter detected 
input   rd_in;          //  Running Disparity
output  rd_out;         //  Running Disparity
output  char_err;       //  Character Error detected  
output  disp_err;       //  Disparity Error detected  
input   clk;            //  Main Clock  
`ifdef USE_CLK_ENA
input   clk_ena;        //  Enable clk
`endif      
input   rst;            //  Asynchronous reset

reg     [7:0] dout; 
reg     kout; 
reg     char_err; 
reg     disp_err; 
wire    rd_out;

//  Most significant bit of the input data
reg     [3:0] din_msb_del; 
//  Output of the 5b/6b block output
reg     [4:0] data_out_5b; 
//  Output of the 3b/4b block output
reg    [2:0] data_out_3b; 
//  Output of the 3b/4b block output special character
reg     [2:0] data_out_3b_sc_pos; 
//reg     [2:0] data_out_3b_sc_neg; 
//  Special character detected at the 3 bit block output
reg     kout_3b_pos; 
reg     kout_3b_neg; 
//  Special character detected at the 5 bit block output
reg     kout_5b_pos; 
reg     kout_5b_neg; 
//  Error detected at the 3 bit block output
reg    error_3b; 
//  Error detected at the 3 bit block output
wire    error_5b; 
reg     error_5b_pos; 
reg     error_5b_neg; 
//  Error detected at the 3 bit block output
wire    error_kout_5b; 
reg     error_kout_5b_pos; 
reg     error_kout_5b_neg; 
//  Error detected at the 3 bit block output
wire    error_3b_sc; 
reg     error_3b_sc_pos; 
reg     error_3b_sc_neg; 
//  Running disparity at the 5 bit block output
wire    rd_out_5b; 
reg     rd_out_5b_pos; 
reg     rd_out_5b_neg; 
reg     rd_out_5b_posx; // extra, when 000111
reg     rd_out_5b_negx; // extra, when 111000

wire    [4:0] data_out_3b_index;
//  Running disparity at the 3 bit block special character output
reg     rd_out_3b_pos; 
reg     rd_out_3b_neg; 
reg     rd_out_3b_posx; // extra, when 0011
reg     rd_out_3b_negx; // extra, when 1100
//  Exception A7 replace P7
reg     except5_p;      // may be exception situation for 5-bit decoded word
reg     except5_n;      // may be exception situation for 5-bit decoded word
reg     except_pos; 
reg     except_neg;  
reg     except_posi; 
reg     except_negi;  
wire    except_err;     // error
wire    except_okn;   

reg     dp5_p;
reg     dp5_n;
reg     dp3_p;
reg     dp3_n;
//wire    rd6;
//wire    rd4_out;
        
//  Special Character Detection
// ----------------------------

always @(posedge clk or posedge rst)
   begin : p_detection_sc
   if (rst == 1'b 1)
      begin
      kout_5b_pos <= 1'b 0;	
      kout_5b_neg <= 1'b 0;	
      kout_3b_pos <= 1'b 0;	
      kout_3b_neg <= 1'b 0;	
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif      
      
              if (ce == 1'b 1 )
                 begin
                 if (din[5:0] == 6'b 000011)
                    begin
                    kout_5b_neg <= 1'b 1;	
                    end
                 else
                    begin
                    kout_5b_neg <= 1'b 0;	
                    end
                 if (din[5:0] == 6'b 111100)
                    begin
                    kout_5b_pos <= 1'b 1;	
                    end
                 else
                    begin
                    kout_5b_pos <= 1'b 0;	
                    end
                 if (din[9:4] == 6'b 000101)
                    begin
                    kout_3b_neg <= 1'b 1;	
                    end
                 else
                    begin
                    kout_3b_neg <= 1'b 0;	
                    end
                 if (din[9:4] == 6'b 111010)
                    begin
                    kout_3b_pos <= 1'b 1;	
                    end
                 else
                    begin
                    kout_3b_pos <= 1'b 0;	
                    end
                 end
      
         `ifdef USE_CLK_ENA
            end
         `endif      
      
      end
   end

//  Data 6b/5b Decoding
//  -------------------                    

always @(posedge clk or posedge rst)
   begin : p_decoder_5b
   if (rst == 1'b 1)
      begin
      data_out_5b       <= 5'b 00000;	
      data_out_3b       <= 3'b 000;	
      rd_out_5b_pos     <= 1'b 0;	
      rd_out_5b_neg     <= 1'b 0;	
      rd_out_5b_posx    <= 1'b 0;	
      rd_out_5b_negx    <= 1'b 0;	
      error_5b_pos      <= 1'b 0;	
      error_5b_neg      <= 1'b 0;	
      error_kout_5b_pos <= 1'b 0;	
      error_kout_5b_neg <= 1'b 0;	
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif       
      
      if (ce == 1'b 1 )
         begin
         
                case (din[5:0])
                
                        6'h0:  data_out_5b<=5'b 00000;    
                        6'h1:  data_out_5b<=5'b 00000; 
                        6'h2:  data_out_5b<=5'b 00000; 
                        6'h3:  data_out_5b<=5'b 00000; 
                        6'h4:  data_out_5b<=5'b 00000; 
                        6'h5:  data_out_5b<=5'b 01111; 
                        6'h6:  data_out_5b<=5'b 00000; 
                        6'h7:  data_out_5b<=5'b 00111; 
                        6'h8:  data_out_5b<=5'b 00000; 
                        6'h9:  data_out_5b<=5'b 10000; 
                        6'hA:  data_out_5b<=5'b 11111; 
                        6'hB:  data_out_5b<=5'b 01011; 
                        6'hC:  data_out_5b<=5'b 11000; 
                        6'hD:  data_out_5b<=5'b 01101; 
                        6'hE:  data_out_5b<=5'b 01110; 
                        6'hF:  data_out_5b<=5'b 00000; 
                        6'h10: data_out_5b<=5'b 00000; 
                        6'h11: data_out_5b<=5'b 00001; 
                        6'h12: data_out_5b<=5'b 00010; 
                        6'h13: data_out_5b<=5'b 10011; 
                        6'h14: data_out_5b<=5'b 00100; 
                        6'h15: data_out_5b<=5'b 10101; 
                        6'h16: data_out_5b<=5'b 10110; 
                        6'h17: data_out_5b<=5'b 10111; 
                        6'h18: data_out_5b<=5'b 01000; 
                        6'h19: data_out_5b<=5'b 11001; 
                        6'h1A: data_out_5b<=5'b 11010; 
                        6'h1B: data_out_5b<=5'b 11011; 
                        6'h1C: data_out_5b<=5'b 11100; 
                        6'h1D: data_out_5b<=5'b 11101; 
                        6'h1E: data_out_5b<=5'b 11110; 
                        6'h1F: data_out_5b<=5'b 00000; 
                        6'h20: data_out_5b<=5'b 00000; 
                        6'h21: data_out_5b<=5'b 11110; 
                        6'h22: data_out_5b<=5'b 11101; 
                        6'h23: data_out_5b<=5'b 00011; 
                        6'h24: data_out_5b<=5'b 11011; 
                        6'h25: data_out_5b<=5'b 00101; 
                        6'h26: data_out_5b<=5'b 00110; 
                        6'h27: data_out_5b<=5'b 01000; 
                        6'h28: data_out_5b<=5'b 10111; 
                        6'h29: data_out_5b<=5'b 01001; 
                        6'h2A: data_out_5b<=5'b 01010; 
                        6'h2B: data_out_5b<=5'b 00100; 
                        6'h2C: data_out_5b<=5'b 01100; 
                        6'h2D: data_out_5b<=5'b 00010; 
                        6'h2E: data_out_5b<=5'b 00001; 
                        6'h2F: data_out_5b<=5'b 00000; 
                        6'h30: data_out_5b<=5'b 00000; 
                        6'h31: data_out_5b<=5'b 10001; 
                        6'h32: data_out_5b<=5'b 10010; 
                        6'h33: data_out_5b<=5'b 11000; 
                        6'h34: data_out_5b<=5'b 10100; 
                        6'h35: data_out_5b<=5'b 11111; 
                        6'h36: data_out_5b<=5'b 10000; 
                        6'h37: data_out_5b<=5'b 00000; 
                        6'h38: data_out_5b<=5'b 00111; 
                        6'h39: data_out_5b<=5'b 00000; 
                        6'h3A: data_out_5b<=5'b 01111; 
                        6'h3B: data_out_5b<=5'b 00000; 
                        6'h3C: data_out_5b<=5'b 00000; 
                        6'h3D: data_out_5b<=5'b 00000; 
                        6'h3E: data_out_5b<=5'b 00000; 
                        6'h3F: data_out_5b<=5'b 00000; 
                        
                endcase 
                        

                rd_out_5b_pos <= dp5_p;
                                         
                rd_out_5b_neg <= dp5_n;

                // special cases, end in p/n although they are neutral (verification)
        
                if( din[5:0] == 6'b 000111 )
                begin
                        rd_out_5b_negx <= 1'b 1;
                end
                else
                begin
                        rd_out_5b_negx <= 1'b 0;
                end
                
                if( din[5:0] == 6'b 111000 )
                begin
                        rd_out_5b_posx <= 1'b 1;
                end
                else
                begin
                        rd_out_5b_posx <= 1'b 0;
                end
                        
                case (din[5:0])
                
                        6'h0: error_5b_pos<=1'b 1; 
                        6'h1: error_5b_pos<=1'b 1; 
                        6'h2: error_5b_pos<=1'b 1; 
                        6'h3: error_5b_pos<=1'b 1; 
                        6'h4: error_5b_pos<=1'b 1; 
                        6'h5: error_5b_pos<=1'b 0; 
                        6'h6: error_5b_pos<=1'b 0; 
                        6'h7: error_5b_pos<=1'b 1; 
                        6'h8: error_5b_pos<=1'b 1; 
                        6'h9: error_5b_pos<=1'b 0; 
                        6'hA: error_5b_pos<=1'b 0; 
                        6'hB: error_5b_pos<=1'b 0; 
                        6'hC: error_5b_pos<=1'b 0; 
                        6'hD: error_5b_pos<=1'b 0; 
                        6'hE: error_5b_pos<=1'b 0; 
                        6'hF: error_5b_pos<=1'b 1; 
                        6'h10: error_5b_pos<=1'b 1;
                        6'h11: error_5b_pos<=1'b 0;
                        6'h12: error_5b_pos<=1'b 0;
                        6'h13: error_5b_pos<=1'b 0;
                        6'h14: error_5b_pos<=1'b 0;
                        6'h15: error_5b_pos<=1'b 0;
                        6'h16: error_5b_pos<=1'b 0;
                        6'h17: error_5b_pos<=1'b 1;
                        6'h18: error_5b_pos<=1'b 0;
                        6'h19: error_5b_pos<=1'b 0;
                        6'h1A: error_5b_pos<=1'b 0;
                        6'h1B: error_5b_pos<=1'b 1;
                        6'h1C: error_5b_pos<=1'b 0;
                        6'h1D: error_5b_pos<=1'b 1;
                        6'h1E: error_5b_pos<=1'b 1;
                        6'h1F: error_5b_pos<=1'b 1;
                        6'h20: error_5b_pos<=1'b 1;
                        6'h21: error_5b_pos<=1'b 0;
                        6'h22: error_5b_pos<=1'b 0;
                        6'h23: error_5b_pos<=1'b 0;
                        6'h24: error_5b_pos<=1'b 0;
                        6'h25: error_5b_pos<=1'b 0;
                        6'h26: error_5b_pos<=1'b 0;
                        6'h27: error_5b_pos<=1'b 1;
                        6'h28: error_5b_pos<=1'b 0;
                        6'h29: error_5b_pos<=1'b 0;
                        6'h2A: error_5b_pos<=1'b 0;
                        6'h2B: error_5b_pos<=1'b 1;
                        6'h2C: error_5b_pos<=1'b 0;
                        6'h2D: error_5b_pos<=1'b 1;
                        6'h2E: error_5b_pos<=1'b 1;
                        6'h2F: error_5b_pos<=1'b 1;
                        6'h30: error_5b_pos<=1'b 1;
                        6'h31: error_5b_pos<=1'b 0;
                        6'h32: error_5b_pos<=1'b 0;
                        6'h33: error_5b_pos<=1'b 1;
                        6'h34: error_5b_pos<=1'b 0;
                        6'h35: error_5b_pos<=1'b 1;
                        6'h36: error_5b_pos<=1'b 1;
                        6'h37: error_5b_pos<=1'b 1;
                        6'h38: error_5b_pos<=1'b 0;
                        6'h39: error_5b_pos<=1'b 1;
                        6'h3A: error_5b_pos<=1'b 1;
                        6'h3B: error_5b_pos<=1'b 1;
                        6'h3C: error_5b_pos<=1'b 1;
                        6'h3D: error_5b_pos<=1'b 1;
                        6'h3E: error_5b_pos<=1'b 1;
                        6'h3F: error_5b_pos<=1'b 1;    
                        
                endcase                    
                                                        
                case(din[5:0])
                
                        6'h0: error_5b_neg<=1'b 1; 
                        6'h1: error_5b_neg<=1'b 1; 
                        6'h2: error_5b_neg<=1'b 1; 
                        6'h3: error_5b_neg<=1'b 1; 
                        6'h4: error_5b_neg<=1'b 1; 
                        6'h5: error_5b_neg<=1'b 1; 
                        6'h6: error_5b_neg<=1'b 1; 
                        6'h7: error_5b_neg<=1'b 0; 
                        6'h8: error_5b_neg<=1'b 1; 
                        6'h9: error_5b_neg<=1'b 1; 
                        6'hA: error_5b_neg<=1'b 1; 
                        6'hB: error_5b_neg<=1'b 0; 
                        6'hC: error_5b_neg<=1'b 1; 
                        6'hD: error_5b_neg<=1'b 0; 
                        6'hE: error_5b_neg<=1'b 0; 
                        6'hF: error_5b_neg<=1'b 1; 
                        6'h10: error_5b_neg<=1'b 1;
                        6'h11: error_5b_neg<=1'b 1;
                        6'h12: error_5b_neg<=1'b 1;
                        6'h13: error_5b_neg<=1'b 0;
                        6'h14: error_5b_neg<=1'b 1;
                        6'h15: error_5b_neg<=1'b 0;
                        6'h16: error_5b_neg<=1'b 0;
                        6'h17: error_5b_neg<=1'b 0;
                        6'h18: error_5b_neg<=1'b 1;
                        6'h19: error_5b_neg<=1'b 0;
                        6'h1A: error_5b_neg<=1'b 0;
                        6'h1B: error_5b_neg<=1'b 0;
                        6'h1C: error_5b_neg<=1'b 0;
                        6'h1D: error_5b_neg<=1'b 0;
                        6'h1E: error_5b_neg<=1'b 0;
                        6'h1F: error_5b_neg<=1'b 1;
                        6'h20: error_5b_neg<=1'b 1;
                        6'h21: error_5b_neg<=1'b 1;
                        6'h22: error_5b_neg<=1'b 1;
                        6'h23: error_5b_neg<=1'b 0;
                        6'h24: error_5b_neg<=1'b 1;
                        6'h25: error_5b_neg<=1'b 0;
                        6'h26: error_5b_neg<=1'b 0;
                        6'h27: error_5b_neg<=1'b 0;
                        6'h28: error_5b_neg<=1'b 1;
                        6'h29: error_5b_neg<=1'b 0;
                        6'h2A: error_5b_neg<=1'b 0;
                        6'h2B: error_5b_neg<=1'b 0;
                        6'h2C: error_5b_neg<=1'b 0;
                        6'h2D: error_5b_neg<=1'b 0;
                        6'h2E: error_5b_neg<=1'b 0;
                        6'h2F: error_5b_neg<=1'b 1;
                        6'h30: error_5b_neg<=1'b 1;
                        6'h31: error_5b_neg<=1'b 0;
                        6'h32: error_5b_neg<=1'b 0;
                        6'h33: error_5b_neg<=1'b 0;
                        6'h34: error_5b_neg<=1'b 0;
                        6'h35: error_5b_neg<=1'b 0;
                        6'h36: error_5b_neg<=1'b 0;
                        6'h37: error_5b_neg<=1'b 1;
                        6'h38: error_5b_neg<=1'b 1;
                        6'h39: error_5b_neg<=1'b 0;
                        6'h3A: error_5b_neg<=1'b 0;
                        6'h3B: error_5b_neg<=1'b 1;
                        6'h3C: error_5b_neg<=1'b 1;
                        6'h3D: error_5b_neg<=1'b 1;
                        6'h3E: error_5b_neg<=1'b 1;
                        6'h3F: error_5b_neg<=1'b 1;
                        
                endcase                                                           
                
                case(din[5:0])
                
                        6'h0: error_kout_5b_pos<=1'b 1; 
                        6'h1: error_kout_5b_pos<=1'b 1; 
                        6'h2: error_kout_5b_pos<=1'b 1; 
                        6'h3: error_kout_5b_pos<=1'b 1; 
                        6'h4: error_kout_5b_pos<=1'b 1; 
                        6'h5: error_kout_5b_pos<=1'b 1; 
                        6'h6: error_kout_5b_pos<=1'b 1; 
                        6'h7: error_kout_5b_pos<=1'b 1; 
                        6'h8: error_kout_5b_pos<=1'b 1; 
                        6'h9: error_kout_5b_pos<=1'b 1; 
                        6'hA: error_kout_5b_pos<=1'b 1; 
                        6'hB: error_kout_5b_pos<=1'b 1; 
                        6'hC: error_kout_5b_pos<=1'b 1; 
                        6'hD: error_kout_5b_pos<=1'b 1; 
                        6'hE: error_kout_5b_pos<=1'b 1; 
                        6'hF: error_kout_5b_pos<=1'b 1; 
                        6'h10: error_kout_5b_pos<=1'b 1;
                        6'h11: error_kout_5b_pos<=1'b 1;
                        6'h12: error_kout_5b_pos<=1'b 1;
                        6'h13: error_kout_5b_pos<=1'b 1;
                        6'h14: error_kout_5b_pos<=1'b 1;
                        6'h15: error_kout_5b_pos<=1'b 1;
                        6'h16: error_kout_5b_pos<=1'b 1;
                        6'h17: error_kout_5b_pos<=1'b 1;
                        6'h18: error_kout_5b_pos<=1'b 1;
                        6'h19: error_kout_5b_pos<=1'b 1;
                        6'h1A: error_kout_5b_pos<=1'b 1;
                        6'h1B: error_kout_5b_pos<=1'b 1;
                        6'h1C: error_kout_5b_pos<=1'b 1;
                        6'h1D: error_kout_5b_pos<=1'b 1;
                        6'h1E: error_kout_5b_pos<=1'b 1;
                        6'h1F: error_kout_5b_pos<=1'b 1;
                        6'h20: error_kout_5b_pos<=1'b 1;
                        6'h21: error_kout_5b_pos<=1'b 0;
                        6'h22: error_kout_5b_pos<=1'b 0;
                        6'h23: error_kout_5b_pos<=1'b 1;
                        6'h24: error_kout_5b_pos<=1'b 0;
                        6'h25: error_kout_5b_pos<=1'b 1;
                        6'h26: error_kout_5b_pos<=1'b 1;
                        6'h27: error_kout_5b_pos<=1'b 1;
                        6'h28: error_kout_5b_pos<=1'b 0;
                        6'h29: error_kout_5b_pos<=1'b 1;
                        6'h2A: error_kout_5b_pos<=1'b 1;
                        6'h2B: error_kout_5b_pos<=1'b 1;
                        6'h2C: error_kout_5b_pos<=1'b 1;
                        6'h2D: error_kout_5b_pos<=1'b 1;
                        6'h2E: error_kout_5b_pos<=1'b 1;
                        6'h2F: error_kout_5b_pos<=1'b 1;
                        6'h30: error_kout_5b_pos<=1'b 1;
                        6'h31: error_kout_5b_pos<=1'b 1;
                        6'h32: error_kout_5b_pos<=1'b 1;
                        6'h33: error_kout_5b_pos<=1'b 1;
                        6'h34: error_kout_5b_pos<=1'b 1;
                        6'h35: error_kout_5b_pos<=1'b 1;
                        6'h36: error_kout_5b_pos<=1'b 1;
                        6'h37: error_kout_5b_pos<=1'b 1;
                        6'h38: error_kout_5b_pos<=1'b 1;
                        6'h39: error_kout_5b_pos<=1'b 1;
                        6'h3A: error_kout_5b_pos<=1'b 1;
                        6'h3B: error_kout_5b_pos<=1'b 1;
                        6'h3C: error_kout_5b_pos<=1'b 1;
                        6'h3D: error_kout_5b_pos<=1'b 1;
                        6'h3E: error_kout_5b_pos<=1'b 1;
                        6'h3F: error_kout_5b_pos<=1'b 1;     
                        
                endcase                                             
                         
                case(din[5:0])
                
                        6'h0: error_kout_5b_neg<=1'b 1; 
                        6'h1: error_kout_5b_neg<=1'b 1; 
                        6'h2: error_kout_5b_neg<=1'b 1; 
                        6'h3: error_kout_5b_neg<=1'b 1; 
                        6'h4: error_kout_5b_neg<=1'b 1; 
                        6'h5: error_kout_5b_neg<=1'b 1; 
                        6'h6: error_kout_5b_neg<=1'b 1; 
                        6'h7: error_kout_5b_neg<=1'b 1; 
                        6'h8: error_kout_5b_neg<=1'b 1; 
                        6'h9: error_kout_5b_neg<=1'b 1; 
                        6'hA: error_kout_5b_neg<=1'b 1; 
                        6'hB: error_kout_5b_neg<=1'b 1; 
                        6'hC: error_kout_5b_neg<=1'b 1; 
                        6'hD: error_kout_5b_neg<=1'b 1; 
                        6'hE: error_kout_5b_neg<=1'b 1; 
                        6'hF: error_kout_5b_neg<=1'b 1; 
                        6'h10: error_kout_5b_neg<=1'b 1;
                        6'h11: error_kout_5b_neg<=1'b 1;
                        6'h12: error_kout_5b_neg<=1'b 1;
                        6'h13: error_kout_5b_neg<=1'b 1;
                        6'h14: error_kout_5b_neg<=1'b 1;
                        6'h15: error_kout_5b_neg<=1'b 1;
                        6'h16: error_kout_5b_neg<=1'b 1;
                        6'h17: error_kout_5b_neg<=1'b 0;
                        6'h18: error_kout_5b_neg<=1'b 1;
                        6'h19: error_kout_5b_neg<=1'b 1;
                        6'h1A: error_kout_5b_neg<=1'b 1;
                        6'h1B: error_kout_5b_neg<=1'b 0;
                        6'h1C: error_kout_5b_neg<=1'b 1;
                        6'h1D: error_kout_5b_neg<=1'b 0;
                        6'h1E: error_kout_5b_neg<=1'b 0;
                        6'h1F: error_kout_5b_neg<=1'b 1;
                        6'h20: error_kout_5b_neg<=1'b 1;
                        6'h21: error_kout_5b_neg<=1'b 1;
                        6'h22: error_kout_5b_neg<=1'b 1;
                        6'h23: error_kout_5b_neg<=1'b 1;
                        6'h24: error_kout_5b_neg<=1'b 1;
                        6'h25: error_kout_5b_neg<=1'b 1;
                        6'h26: error_kout_5b_neg<=1'b 1;
                        6'h27: error_kout_5b_neg<=1'b 1;
                        6'h28: error_kout_5b_neg<=1'b 1;
                        6'h29: error_kout_5b_neg<=1'b 1;
                        6'h2A: error_kout_5b_neg<=1'b 1;
                        6'h2B: error_kout_5b_neg<=1'b 1;
                        6'h2C: error_kout_5b_neg<=1'b 1;
                        6'h2D: error_kout_5b_neg<=1'b 1;
                        6'h2E: error_kout_5b_neg<=1'b 1;
                        6'h2F: error_kout_5b_neg<=1'b 1;
                        6'h30: error_kout_5b_neg<=1'b 1;
                        6'h31: error_kout_5b_neg<=1'b 1;
                        6'h32: error_kout_5b_neg<=1'b 1;
                        6'h33: error_kout_5b_neg<=1'b 1;
                        6'h34: error_kout_5b_neg<=1'b 1;
                        6'h35: error_kout_5b_neg<=1'b 1;
                        6'h36: error_kout_5b_neg<=1'b 1;
                        6'h37: error_kout_5b_neg<=1'b 1;
                        6'h38: error_kout_5b_neg<=1'b 1;
                        6'h39: error_kout_5b_neg<=1'b 1;
                        6'h3A: error_kout_5b_neg<=1'b 1;
                        6'h3B: error_kout_5b_neg<=1'b 1;
                        6'h3C: error_kout_5b_neg<=1'b 1;
                        6'h3D: error_kout_5b_neg<=1'b 1;
                        6'h3E: error_kout_5b_neg<=1'b 1;
                        6'h3F: error_kout_5b_neg<=1'b 1;   
                        
                endcase       

                case( din[9:6] )
                 
                        4'h0: data_out_3b <= 3'b 000; 
                        4'h1: data_out_3b <= 3'b 111; 
                        4'h2: data_out_3b <= 3'b 000; 
                        4'h3: data_out_3b <= 3'b 011; 
                        4'h4: data_out_3b <= 3'b 100; 
                        4'h5: data_out_3b <= 3'b 101; 
                        4'h6: data_out_3b <= 3'b 110; 
                        4'h7: data_out_3b <= 3'b 111; 
                        4'h8: data_out_3b <= 3'b 111; 
                        4'h9: data_out_3b <= 3'b 001; 
                        4'hA: data_out_3b <= 3'b 010; 
                        4'hB: data_out_3b <= 3'b 100; 
                        4'hC: data_out_3b <= 3'b 011; 
                        4'hD: data_out_3b <= 3'b 000; 
                        4'hE: data_out_3b <= 3'b 111; 
                        4'hF: data_out_3b <= 3'b 000; 
        
                endcase


           
         end
      
         `ifdef USE_CLK_ENA
            end
         `endif       
      
      end
   end
  
// calc rd after 6 bit sub-block:
assign rd_out_5b = (rd_out_5b_neg==1'b 0 & rd_out_5b_pos==1'b 0 & rd_in==1'b 1)  | rd_out_5b_pos == 1'b 1 ? 1'b 1 : 1'b 0; // neutral: copy input, else p/n from block

// calc rd after 4 bit sub-block:
assign rd_out = (rd_out_3b_neg==1'b 0 & rd_out_3b_pos==1'b 0 & rd_out_5b==1'b 1) | rd_out_3b_pos == 1'b 1 ? 1'b 1 : 1'b 0; // neutral: copy input, else p/n from block

assign error_5b      = rd_in       == 1'b 1 ? error_5b_pos      : error_5b_neg; 
assign error_kout_5b = kout_3b_neg == 1'b 0 ? error_kout_5b_pos : error_kout_5b_neg; 

//  Exception Detection
// --------------------

always @(posedge clk or posedge rst)
   begin : p_detection_except
   if (rst == 1'b 1)
      begin
      except_pos <= 1'b 0;	
      except_neg <= 1'b 0;	
      except_posi<= 1'b 0;	
      except_negi<= 1'b 0;	
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif       
      
      if (ce == 1'b 1 )
         begin
         if (din[9:6] == 4'b 0001)      // 0001
            begin
            except_pos <= 1'b 1;	
            end
         else
            begin
            except_pos <= 1'b 0;	
            end
         
         if (din[9:6] == 4'b 1110)      // 1110
            begin
            except_neg <= 1'b 1;	
            end
         else
            begin
            except_neg <= 1'b 0;	
            end

                // inverted check
         if (din[9:6] == 4'b 1000)      
            begin
            except_posi <= 1'b 1;	
            end
         else
            begin
            except_posi <= 1'b 0;	
            end
         
         if (din[9:6] == 4'b 0111)      
            begin
            except_negi <= 1'b 1;	
            end
         else
            begin
            except_negi <= 1'b 0;	
            end

         end
      
         `ifdef USE_CLK_ENA
            end
         `endif      
      
      end
   end

// mark that 6-bit subblock indicates a exception character
always @(posedge clk or posedge rst)
   begin : p_dx5
   if (rst == 1'b 1)
      begin
      except5_p <= 1'b 0;	
      except5_n <= 1'b 0;	
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif      
      
      if (ce == 1'b 1 )
         begin
         if (din[5:0] == 6'b 001011 |  // D11.7
             din[5:0] == 6'b 001101 |  // D13.7
             din[5:0] == 6'b 001110 )  // D14.7
            begin
            except5_p <= 1'b 1;	
            end
         else
            begin
            except5_p <= 1'b 0;	
            end

         if (din[5:0] == 6'b 110001 |  // D17.7
             din[5:0] == 6'b 110010 |  // D18.7
             din[5:0] == 6'b 110100 )  // D20.7
            begin
            except5_n <= 1'b 1;	
            end
         else
            begin
            except5_n <= 1'b 0;	
            end

         end
      
         `ifdef USE_CLK_ENA
            end
         `endif      
      
      end
   end

assign except_err = (except5_n==1'b 1 & except_negi==1'b 1 & rd_in==1'b 0) |                     // exception character with RD-
                    (except5_p==1'b 1 & except_posi==1'b 1 & rd_in==1'b 1) ? 1'b 1 : 1'b 0;      // exception character with RD+

assign except_okn = (except5_n==1'b 1 & except_neg==1'b 1 & rd_in==1'b 0) |                     // create a mask
                    (except5_p==1'b 1 & except_pos==1'b 1 & rd_in==1'b 1) ? 1'b 0 : 1'b 1;

//  The Din MSB must be delayed to be consistent with the except flag
//  -----------------------------------------------------------------  

always @(posedge clk or posedge rst)
   begin : p_din_msb_del
   if (rst == 1'b 1)
      begin
      din_msb_del <= {4{1'b 0}};	
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif      
      
      if (ce == 1'b 1 )
         begin
         din_msb_del <= din[9:6];	
         end
      
         `ifdef USE_CLK_ENA
            end
         `endif      
      
      end
   end

//  Special Character 4b/5b Decoding
//  --------------------------------

assign data_out_3b_index={rd_out_5b, din_msb_del}; // din_msb} ;

always@(data_out_3b_index)
begin

        case(data_out_3b_index)
        
                5'h0: error_3b=1'b 1; 
                5'h1: error_3b=1'b 1; 
                5'h2: error_3b=1'b 1; 
                5'h3: error_3b=1'b 0; 
                5'h4: error_3b=1'b 1; 
                5'h5: error_3b=1'b 0; 
                5'h6: error_3b=1'b 0; 
                5'h7: error_3b=1'b 0; 
                5'h8: error_3b=1'b 1; 
                5'h9: error_3b=1'b 0; 
                5'hA: error_3b=1'b 0; 
                5'hB: error_3b=1'b 0; 
                5'hC: error_3b=1'b 1; 
                5'hD: error_3b=1'b 0; 
                5'hE: error_3b=1'b 1; 
                5'hF: error_3b=1'b 1; 
                5'h10: error_3b=1'b 1;
                5'h11: error_3b=1'b 1;
                5'h12: error_3b=1'b 0;
                5'h13: error_3b=1'b 1;
                5'h14: error_3b=1'b 0;
                5'h15: error_3b=1'b 0;
                5'h16: error_3b=1'b 0;
                5'h17: error_3b=1'b 1;
                5'h18: error_3b=1'b 0;
                5'h19: error_3b=1'b 0;
                5'h1A: error_3b=1'b 0;
                5'h1B: error_3b=1'b 1;
                5'h1C: error_3b=1'b 0;
                5'h1D: error_3b=1'b 1;
                5'h1E: error_3b=1'b 1;
                5'h1F: error_3b=1'b 1;
                
        endcase
        
end

always @(posedge clk or posedge rst)
   begin : p_decoder_sc
   if (rst == 1'b 1)
      begin
      data_out_3b_sc_pos <= {3{1'b 0}};	
      //data_out_3b_sc_neg <= {3{1'b 0}};	
      rd_out_3b_pos   <= 1'b 0;	
      rd_out_3b_posx  <= 1'b 0;
      rd_out_3b_neg   <= 1'b 0;	
      rd_out_3b_negx  <= 1'b 0;	
      error_3b_sc_pos    <= 1'b 0;	
      error_3b_sc_neg    <= 1'b 0;	
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif        
      
      if (ce == 1'b 1 )
         begin
         
         case(din[9:6])
         
                4'h0: data_out_3b_sc_pos <= 3'b 000;
                4'h1: data_out_3b_sc_pos <= 3'b 111; // special(n)
                4'h2: data_out_3b_sc_pos <= 3'b 000;
                4'h3: data_out_3b_sc_pos <= 3'b 011;
                4'h4: data_out_3b_sc_pos <= 3'b 100; // special(n)
                4'h5: data_out_3b_sc_pos <= 3'b 010;
                4'h6: data_out_3b_sc_pos <= 3'b 001;
                4'h7: data_out_3b_sc_pos <= 3'b 111; // special(n)
                4'h8: data_out_3b_sc_pos <= 3'b 111; // special(n)
                4'h9: data_out_3b_sc_pos <= 3'b 110;
                4'hA: data_out_3b_sc_pos <= 3'b 101;
                4'hB: data_out_3b_sc_pos <= 3'b 100;
                4'hC: data_out_3b_sc_pos <= 3'b 011; // special (n)
                4'hD: data_out_3b_sc_pos <= 3'b 000;
                4'hE: data_out_3b_sc_pos <= 3'b 111;
                4'hF: data_out_3b_sc_pos <= 3'b 000;
                
        endcase
        
        rd_out_3b_pos <= dp3_p;
        
        rd_out_3b_neg <= dp3_n;
        
        // special cases, end in p/n although they are neutral (verification)
        
        if( din[9:6] == 4'b 0011 )
        begin
                rd_out_3b_negx <= 1'b 1;
        end
        else
        begin
                rd_out_3b_negx <= 1'b 0;
        end
        
        if( din[9:6] == 4'b 1100 )
        begin
                rd_out_3b_posx <= 1'b 1;
        end
        else
        begin
                rd_out_3b_posx <= 1'b 0;
        end
        
        
        case(din[9:6])
        
                4'h0: error_3b_sc_pos <= 1'b 1;
                4'h1: error_3b_sc_pos <= 1'b 1;
                4'h2: error_3b_sc_pos <= 1'b 1;
                4'h3: error_3b_sc_pos <= 1'b 0;
                4'h4: error_3b_sc_pos <= 1'b 1;
                4'h5: error_3b_sc_pos <= 1'b 0;
                4'h6: error_3b_sc_pos <= 1'b 0;
                4'h7: error_3b_sc_pos <= 1'b 1;
                4'h8: error_3b_sc_pos <= 1'b 1;
                4'h9: error_3b_sc_pos <= 1'b 0;
                4'hA: error_3b_sc_pos <= 1'b 0;
                4'hB: error_3b_sc_pos <= 1'b 0;
                4'hC: error_3b_sc_pos <= 1'b 1;
                4'hD: error_3b_sc_pos <= 1'b 0;
                4'hE: error_3b_sc_pos <= 1'b 0;
                4'hF: error_3b_sc_pos <= 1'b 1;   
                
        endcase
        
        case(din[9:6])
        
                4'h0: error_3b_sc_neg <= 1'b 1;
                4'h1: error_3b_sc_neg <= 1'b 0;
                4'h2: error_3b_sc_neg <= 1'b 0;
                4'h3: error_3b_sc_neg <= 1'b 1;
                4'h4: error_3b_sc_neg <= 1'b 0;
                4'h5: error_3b_sc_neg <= 1'b 0;
                4'h6: error_3b_sc_neg <= 1'b 0;
                4'h7: error_3b_sc_neg <= 1'b 1;
                4'h8: error_3b_sc_neg <= 1'b 1;
                4'h9: error_3b_sc_neg <= 1'b 0;
                4'hA: error_3b_sc_neg <= 1'b 0;
                4'hB: error_3b_sc_neg <= 1'b 1;
                4'hC: error_3b_sc_neg <= 1'b 0;
                4'hD: error_3b_sc_neg <= 1'b 1;
                4'hE: error_3b_sc_neg <= 1'b 1;
                4'hF: error_3b_sc_neg <= 1'b 1;  
                
        endcase                      
        
          end
      
         `ifdef USE_CLK_ENA
            end
         `endif      
      
      end
   end

assign error_3b_sc = kout_5b_neg == 1'b 1 ? error_3b_sc_pos : error_3b_sc_neg; 

//  Error Decoding - Disparity and Code Group Error
// ------------------------------------------------

always @(posedge clk or posedge rst)
   begin : p_output
   if (rst == 1'b 1)
      begin
      dout     <= {8{1'b 0}};   
      kout     <= 1'b 0;   
      char_err <= 1'b 0; 
      end
   else
      begin
      
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif      
      
      if (ce == 1'b 1 )
         begin
            if(sync==1'b 0)
               begin
               kout     <= 1'b 0;   
               dout     <= 8'h 00;   
               char_err <= 1'b 1;
               end                                
            else if (kout_5b_neg == 1'b 1)
               begin
               kout     <= 1'b 1;   
               dout     <= {data_out_3b_sc_pos, 5'b 11100};   
               char_err <= error_3b_sc | !rd_in; 
               end
            else if (kout_5b_pos == 1'b 1 )
               begin
               kout     <= 1'b 1;   
               dout     <= {data_out_3b, 5'b 11100};   // sc_neg is identical to 3b
               char_err <= error_3b_sc | rd_in;
               end
            else if (kout_3b_neg == 1'b 1 )
               begin
               kout     <= 1'b 1;   
               dout     <= {3'b 111, data_out_5b};   
               char_err <= error_kout_5b;
               end
            else if (kout_3b_pos == 1'b 1 )
               begin
               kout     <= 1'b 1;   
               dout     <= {3'b 111, data_out_5b};   
               char_err <= error_kout_5b; 
               end
            else
               begin
               kout     <= 1'b 0;   
               dout     <= {data_out_3b, data_out_5b};   
               char_err <= error_5b | (error_3b & except_okn) | except_err;
               end
            end
      
         `ifdef USE_CLK_ENA
            end
         `endif      
      
      end
   end

always @(posedge clk or posedge rst)
begin : p_de
        if (rst == 1'b 1)
        begin
                disp_err <= 1'b 0;
        end
        else
        begin
                
         `ifdef USE_CLK_ENA
            if(clk_ena == 1'b 1)
            begin
         `endif                
                
                if (ce == 1'b 1 )
                begin
                        
                        if( (rd_in==1'b 1 & rd_out_5b_pos==1'b 1 & rd_out_5b_posx==1'b 0) |     // pos cannot be followed by pos
                            (rd_in==1'b 0 & rd_out_5b_neg==1'b 1 & rd_out_5b_negx==1'b 0) |     // neg cannot not followed by neg
                            (rd_out_5b==1'b 1 & rd_out_3b_pos==1'b 1 & rd_out_3b_posx==1'b 0) | // pos cannot be followed by pos
                            (rd_out_5b==1'b 0 & rd_out_3b_neg==1'b 1 & rd_out_3b_negx==1'b 0) | // neg cannot not followed by neg
                            (rd_in==1'b 0 & rd_out_5b_posx==1'b 1) |    // special allowed only when input is positive
                            (rd_in==1'b 1 & rd_out_5b_negx==1'b 1) |    // special allowed only when input is negative
                            (rd_out_5b==1'b 0 & rd_out_3b_posx==1'b 1) | // special allowed only when input is positive
                            (rd_out_5b==1'b 1 & rd_out_3b_negx==1'b 1) ) // special allowed only when input is positive
                        begin
                        
                                disp_err <= 1'b 1;
                        end
                        else
                        begin
                                disp_err <= 1'b 0;
                        end

                end
        
         `ifdef USE_CLK_ENA
            end
         `endif        
        
        end
end



// 6-bit disparity calc
always @(din)
begin
        case(din[5:0])
        
                6'b 000000: dp5_p = 1'b 0;
                6'b 000001: dp5_p = 1'b 0;
                6'b 000010: dp5_p = 1'b 0;
                6'b 000011: dp5_p = 1'b 0;
                6'b 000100: dp5_p = 1'b 0;
                6'b 000101: dp5_p = 1'b 0;
                6'b 000110: dp5_p = 1'b 0;
                6'b 000111: dp5_p = 1'b 0;
                6'b 001000: dp5_p = 1'b 0;
                6'b 001001: dp5_p = 1'b 0;
                6'b 001010: dp5_p = 1'b 0;
                6'b 001011: dp5_p = 1'b 0;
                6'b 001100: dp5_p = 1'b 0;
                6'b 001101: dp5_p = 1'b 0;
                6'b 001110: dp5_p = 1'b 0;
                6'b 001111: dp5_p = 1'b 1;

                6'b 010000: dp5_p = 1'b 0;
                6'b 010001: dp5_p = 1'b 0;
                6'b 010010: dp5_p = 1'b 0;
                6'b 010011: dp5_p = 1'b 0;
                6'b 010100: dp5_p = 1'b 0;
                6'b 010101: dp5_p = 1'b 0;
                6'b 010110: dp5_p = 1'b 0;
                6'b 010111: dp5_p = 1'b 1;
                6'b 011000: dp5_p = 1'b 0;
                6'b 011001: dp5_p = 1'b 0;
                6'b 011010: dp5_p = 1'b 0;
                6'b 011011: dp5_p = 1'b 1;
                6'b 011100: dp5_p = 1'b 0;
                6'b 011101: dp5_p = 1'b 1;
                6'b 011110: dp5_p = 1'b 1;
                6'b 011111: dp5_p = 1'b 1;
                
                6'b 100000: dp5_p = 1'b 0;
                6'b 100001: dp5_p = 1'b 0;
                6'b 100010: dp5_p = 1'b 0;
                6'b 100011: dp5_p = 1'b 0;
                6'b 100100: dp5_p = 1'b 0;
                6'b 100101: dp5_p = 1'b 0;
                6'b 100110: dp5_p = 1'b 0;
                6'b 100111: dp5_p = 1'b 1;
                6'b 101000: dp5_p = 1'b 0;
                6'b 101001: dp5_p = 1'b 0;
                6'b 101010: dp5_p = 1'b 0;
                6'b 101011: dp5_p = 1'b 1;
                6'b 101100: dp5_p = 1'b 0;
                6'b 101101: dp5_p = 1'b 1;
                6'b 101110: dp5_p = 1'b 1;
                6'b 101111: dp5_p = 1'b 1;

                6'b 110000: dp5_p = 1'b 0;
                6'b 110001: dp5_p = 1'b 0;
                6'b 110010: dp5_p = 1'b 0;
                6'b 110011: dp5_p = 1'b 1;
                6'b 110100: dp5_p = 1'b 0;
                6'b 110101: dp5_p = 1'b 1;
                6'b 110110: dp5_p = 1'b 1;
                6'b 110111: dp5_p = 1'b 1;
                6'b 111000: dp5_p = 1'b 1;  // special
                6'b 111001: dp5_p = 1'b 1;
                6'b 111010: dp5_p = 1'b 1;
                6'b 111011: dp5_p = 1'b 1;
                6'b 111100: dp5_p = 1'b 1;
                6'b 111101: dp5_p = 1'b 1;
                6'b 111110: dp5_p = 1'b 1;
                6'b 111111: dp5_p = 1'b 1;

        endcase                      
end

always @(din)
begin
        case(din[5:0])
        
                6'b 000000: dp5_n = 1'b 1;
                6'b 000001: dp5_n = 1'b 1;
                6'b 000010: dp5_n = 1'b 1;
                6'b 000011: dp5_n = 1'b 1;
                6'b 000100: dp5_n = 1'b 1;
                6'b 000101: dp5_n = 1'b 1;
                6'b 000110: dp5_n = 1'b 1;
                6'b 000111: dp5_n = 1'b 1;  // special
                6'b 001000: dp5_n = 1'b 1;
                6'b 001001: dp5_n = 1'b 1;
                6'b 001010: dp5_n = 1'b 1;
                6'b 001011: dp5_n = 1'b 0;
                6'b 001100: dp5_n = 1'b 1;
                6'b 001101: dp5_n = 1'b 0;
                6'b 001110: dp5_n = 1'b 0;
                6'b 001111: dp5_n = 1'b 0;

                6'b 010000: dp5_n = 1'b 1;
                6'b 010001: dp5_n = 1'b 1;
                6'b 010010: dp5_n = 1'b 1;
                6'b 010011: dp5_n = 1'b 0;
                6'b 010100: dp5_n = 1'b 1;
                6'b 010101: dp5_n = 1'b 0;
                6'b 010110: dp5_n = 1'b 0;
                6'b 010111: dp5_n = 1'b 0;
                6'b 011000: dp5_n = 1'b 1;
                6'b 011001: dp5_n = 1'b 0;
                6'b 011010: dp5_n = 1'b 0;
                6'b 011011: dp5_n = 1'b 0;
                6'b 011100: dp5_n = 1'b 0;
                6'b 011101: dp5_n = 1'b 0;
                6'b 011110: dp5_n = 1'b 0;
                6'b 011111: dp5_n = 1'b 0;
                
                6'b 100000: dp5_n = 1'b 1;
                6'b 100001: dp5_n = 1'b 1;
                6'b 100010: dp5_n = 1'b 1;
                6'b 100011: dp5_n = 1'b 0;
                6'b 100100: dp5_n = 1'b 1;
                6'b 100101: dp5_n = 1'b 0;
                6'b 100110: dp5_n = 1'b 0;
                6'b 100111: dp5_n = 1'b 0;
                6'b 101000: dp5_n = 1'b 1;
                6'b 101001: dp5_n = 1'b 0;
                6'b 101010: dp5_n = 1'b 0;
                6'b 101011: dp5_n = 1'b 0;
                6'b 101100: dp5_n = 1'b 0;
                6'b 101101: dp5_n = 1'b 0;
                6'b 101110: dp5_n = 1'b 0;
                6'b 101111: dp5_n = 1'b 0;

                6'b 110000: dp5_n = 1'b 1;
                6'b 110001: dp5_n = 1'b 0;
                6'b 110010: dp5_n = 1'b 0;
                6'b 110011: dp5_n = 1'b 0;
                6'b 110100: dp5_n = 1'b 0;
                6'b 110101: dp5_n = 1'b 0;
                6'b 110110: dp5_n = 1'b 0;
                6'b 110111: dp5_n = 1'b 0;
                6'b 111000: dp5_n = 1'b 0;
                6'b 111001: dp5_n = 1'b 0;
                6'b 111010: dp5_n = 1'b 0;
                6'b 111011: dp5_n = 1'b 0;
                6'b 111100: dp5_n = 1'b 0;
                6'b 111101: dp5_n = 1'b 0;
                6'b 111110: dp5_n = 1'b 0;
                6'b 111111: dp5_n = 1'b 0;

        endcase                      
end

// 4-bit disparity calc

always @(din)
begin
        case(din[9:6])
        
                4'b 0000: dp3_p = 1'b 0;
                4'b 0001: dp3_p = 1'b 0;
                4'b 0010: dp3_p = 1'b 0;
                4'b 0011: dp3_p = 1'b 0;
                4'b 0100: dp3_p = 1'b 0;
                4'b 0101: dp3_p = 1'b 0;
                4'b 0110: dp3_p = 1'b 0;
                4'b 0111: dp3_p = 1'b 1;
                4'b 1000: dp3_p = 1'b 0;
                4'b 1001: dp3_p = 1'b 0;
                4'b 1010: dp3_p = 1'b 0;
                4'b 1011: dp3_p = 1'b 1;
                4'b 1100: dp3_p = 1'b 1;  // special
                4'b 1101: dp3_p = 1'b 1;
                4'b 1110: dp3_p = 1'b 1;
                4'b 1111: dp3_p = 1'b 1;
                
        endcase
end

always @(din)
begin
        case(din[9:6])
        
                4'b 0000: dp3_n = 1'b 1;
                4'b 0001: dp3_n = 1'b 1;
                4'b 0010: dp3_n = 1'b 1;
                4'b 0011: dp3_n = 1'b 1;  // special
                4'b 0100: dp3_n = 1'b 1;
                4'b 0101: dp3_n = 1'b 0;
                4'b 0110: dp3_n = 1'b 0;
                4'b 0111: dp3_n = 1'b 0;
                4'b 1000: dp3_n = 1'b 1;
                4'b 1001: dp3_n = 1'b 0;
                4'b 1010: dp3_n = 1'b 0;
                4'b 1011: dp3_n = 1'b 0;
                4'b 1100: dp3_n = 1'b 0;
                4'b 1101: dp3_n = 1'b 0;
                4'b 1110: dp3_n = 1'b 0;
                4'b 1111: dp3_n = 1'b 0;
        endcase
end

endmodule // module dec_func_xgxs